package io.gitee.zhangbinhub.acp.core.common.base

import io.gitee.zhangbinhub.acp.core.common.security.ByteEncodeEnum
import io.gitee.zhangbinhub.acp.core.common.tools.CommonUtils
import org.bouncycastle.util.encoders.Base64
import org.bouncycastle.util.encoders.Hex

abstract class BaseEncrypt {
    companion object {
        @JvmStatic
        protected fun decodeToBytes(text: String, byteEncode: ByteEncodeEnum): ByteArray = when (byteEncode) {
            ByteEncodeEnum.HEX -> Hex.decode(text)
            ByteEncodeEnum.BASE64 -> Base64.decode(text)
            ByteEncodeEnum.BYTE -> text.toByteArray(CommonUtils.defaultCharset)
        }

        @JvmStatic
        protected fun encodeToString(bytes: ByteArray, byteEncode: ByteEncodeEnum): String = when (byteEncode) {
            ByteEncodeEnum.HEX -> Hex.toHexString(bytes)
            ByteEncodeEnum.BASE64 -> Base64.toBase64String(bytes)
            ByteEncodeEnum.BYTE -> String(bytes, CommonUtils.defaultCharset)
        }
    }
}